// import { handleArea } from '@basic/area/area'

import Vue from "vue";

const MUTATION_TYPES = {
  UPDATE_GETTER_FLAG: 'UPDATE_GETTER_FLAG',
  CLEAR_RMS_NEW_RECORD_SEARCH: 'CLEAR_RMS_NEW_RECORD_SEARCH',
  FIRST_NEW_RECORDS_SEARCH: 'FIRST_NEW_RECORDS_SEARCH'
};

const state = {
  filters: null,
  firstNewRecordSearch: {},
  advanceNewRecordSearch: {},
  updateGetterFlag: null
};

const getters = {
  getRmsFirstRecordSearch: (state, getters) => (userId, menuId) => {
    // console.log('Should update getter?', state.updateGetterFlag, 10086);
    if (state.updateGetterFlag) {
      const updatedRecordSearch = getters.getRmsFirstRecordSearch(userId, menuId);
      return updatedRecordSearch;
    } else {
      // console.log(state.firstNewRecordSearch,100000086666)
      return state.firstNewRecordSearch?.[userId]?.[menuId] || {};
    }
  },
};

const mutations = {
  [MUTATION_TYPES.FIRST_NEW_RECORDS_SEARCH] (state, { userId, menuId, firstNewRecordSearch = {} }) {
    if (!state.firstNewRecordSearch[userId]) {
      state.firstNewRecordSearch[userId] = {};
    }
    state.firstNewRecordSearch[userId][menuId] = firstNewRecordSearch;
  },
  [MUTATION_TYPES.UPDATE_GETTER_FLAG] (state, flag) {
    state.updateGetterFlag = flag;
  },
  [MUTATION_TYPES.CLEAR_RMS_NEW_RECORD_SEARCH] (state, userId) {
    if (state.firstNewRecordSearch[userId]) {
      state.firstNewRecordSearch[userId] = {};
    }
    if (state.advanceNewRecordSearch[userId]) {
      state.advanceNewRecordSearch[userId] = {};
    }
    state.records = []
    state.columns = []
  },
};

const actions = {
  async recordRmsNewFilters ({ commit, state }, { userId, menuId, data }) {
    const transformFilters = data
    // 更新updateGetterFlag为true，表示需要重新计算getter
    commit(MUTATION_TYPES.UPDATE_GETTER_FLAG, true);
    commit(MUTATION_TYPES.FIRST_NEW_RECORDS_SEARCH, { userId, menuId, firstNewRecordSearch: transformFilters });
    // 更新完tags后，将updateGetterFlag重新设置为false，停止重新计算getter
    commit(MUTATION_TYPES.UPDATE_GETTER_FLAG, false);
  },
  async clearRmsNewRecordSearch ({ commit }, userId) {
    commit(MUTATION_TYPES.CLEAR_RMS_NEW_RECORD_SEARCH, userId);
    commit(MUTATION_TYPES.UPDATE_GETTER_FLAG, false);
  }
};

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
};
